//
//  XCRadioButtonViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/11.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCRadioButtonViewController.h"
#import "XCRadioButtonCollectionViewCell.h"
#import "XCReuseHeadCollectionReusableView.h"
#import "XCAFNetTool.h"
#import "XCUrlManger.h"
#import "XCRadioFirDataModel.h"
#import "XCInTheRadioDataModel.h"
#import "OnlineAVplay.h"
#import "XCInTheRadioViewController.h"
@interface XCRadioButtonViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@end

@implementation XCRadioButtonViewController

- (void)dealloc {
    
    _collectionView.dataSource = nil;
    _collectionView.delegate = nil;
    
    [_collectionView release];
    [_dataSource release];

    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   
    self.navigationItem.title = @"电台";
    [self netRequesrt];
    [self collectionBuildAction];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
}
#pragma mark - netRequest
- (void)netRequesrt {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger RadioNetRequest] body:nil headerFile:nil response:XCJSON success:^(id result) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"data"]) {
            XCRadioFirDataModel *radioFirDataModel = [[XCRadioFirDataModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:radioFirDataModel];
            [radioFirDataModel release];
        }
        
//        NSLog(@"%@", self.dataSource);
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
    }];
}
#pragma mark - collection 的创建
- (void)collectionBuildAction {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(wide / 2 - 20, wide / 4);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 5);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    注册 cell 重用池
    [self.collectionView registerClass:[XCRadioButtonCollectionViewCell class] forCellWithReuseIdentifier:@"XCRadioButtonCollectionViewCellIdentifier"];
//    注册 头视图 重用池
    // 头视图的重用池
    [self.collectionView registerClass:[XCReuseHeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseHeader"];
    [self.view addSubview:self.collectionView];
    
    
}
#pragma mark - collectionDelegate 
// 区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.dataSource) {
    return self.dataSource.count;
    }
    return 0;
}
// 行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    XCRadioFirDataModel *radioFirDataModel = self.dataSource[section];
    
    return radioFirDataModel.mutableArray.count;
}
// item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XCRadioButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XCRadioButtonCollectionViewCellIdentifier" forIndexPath:indexPath];
    XCRadioFirDataModel *radioFirDataModel = self.dataSource[indexPath.section];
    
    cell.radioSecModel = radioFirDataModel.mutableArray[indexPath.row];

    return cell;
}
#pragma mark 创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        XCReuseHeadCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ReuseHeader" forIndexPath:indexPath];
        
        XCRadioFirDataModel *radioFirDataModel = self.dataSource[indexPath.section];
        header.nameLable.text = radioFirDataModel.tag_type_name;
        return header;
    }
    return nil;
}
#pragma mark 头视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

    return CGSizeMake(wide, 21);
}

#pragma mark - item 的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//     self.numberOfSong = 0;
    XCRadioFirDataModel *radioFirDataModel = self.dataSource[indexPath.section];
    XCRadioSecDataModel *radioSecModel = radioFirDataModel.mutableArray[indexPath.row];

    XCInTheRadioViewController *radioVC = [[XCInTheRadioViewController alloc] initWithTag:radioSecModel.tag_id WithTagName:radioSecModel.tag_name];
    [self.navigationController pushViewController:radioVC animated:YES];

    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
